//
//  ControlStyle.swift
//  Alamofire
//
//  Created by Fanxx on 2019/7/16.
//

import UIKit

extension UCSetting where TargetType: UIControl {
    ///对齐
    public static func align(horizontal align:UIControl.ContentHorizontalAlignment) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.contentHorizontalAlignment = align
        })
    }
    ///竖起方向对齐
    public static func align(vertical align:UIControl.ContentVerticalAlignment) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.contentVerticalAlignment = align
        })
    }
    ///选中
    public static func selected(_ selected:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isSelected = selected
        })
    }
    ///启用
    public static func enabled(_ enabled:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isEnabled = enabled
        })
    }
    ///高亮
    public static func highlighted(_ isHighlighted:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isHighlighted = isHighlighted
        })
    }
    ///居中对齐
    public static var center: UCSetting<TargetType> {
        return .init(action: { (target) in
            target.contentHorizontalAlignment = .center
            target.contentVerticalAlignment = .center
        })
    }
    ///左对齐
    public static var left: UCSetting<TargetType> {
        return self.align(horizontal: .left)
    }
    ///右对齐
    public static var right: UCSetting<TargetType> {
        return self.align(horizontal: .right)
    }
    ///上对齐
    public static var top: UCSetting<TargetType> {
        return self.align(vertical: .top)
    }
    ///下对齐
    public static var bottom: UCSetting<TargetType> {
        return self.align(vertical: .bottom)
    }
    ///事件
    public static func event(_ target:Any?, _ action: Selector, for event: UIControl.Event = .touchUpInside) -> UCSetting<TargetType> {
        return .init(action: { (obj) in
            obj.addTarget(target, action: action, for: event)
        })
    }
    ///事件: 值变化
    public static func valueChanged(_ target:Any?, _ action: Selector) -> UCSetting<TargetType> {
        return event(target, action, for: .valueChanged)
    }
    ///事件: 编辑中
    public static func editing(_ target:Any?, _ action: Selector) -> UCSetting<TargetType> {
        return event(target, action, for: .editingChanged)
    }
    ///事件: 编辑结束
    public static func edited(_ target:Any?, _ action: Selector) -> UCSetting<TargetType> {
        return event(target, action, for: .editingDidEnd)
    }
    ///事件: 点击
    public static func tap(_ target:Any?, _ action: Selector) -> UCSetting<TargetType> {
        return event(target, action, for: .touchUpInside)
    }
}
